package com.tjn;

import java.util.Scanner;

/**
 * @author 陶镓妮
 * @version 1.0
 */



public class Exercise01 {
    public static void main(String[] args) throws InterruptedException {
        A a = new A();
        B b = new B(a);
        a.start();
        b.start();
    }
}

//在main方法中启动两个线程，第一个线程循环随机打印100以内的整数，知道第二个线程从键盘读取了“Q"命令
class A extends Thread{
    private boolean loop = true;

    @Override
    public void run() {
        while(loop){
            int number = (int)(Math.random() * 100);
            System.out.println("随机数为：" + number);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void setLoop(boolean loop) {
        this.loop = loop;
    }
}

class B extends Thread{
    private A a;
    Scanner scanner = new Scanner(System.in);
    public B(A a) {
        this.a = a;
    }

    @Override
    public void run() {
        while(true){
            System.out.println("输入Q结束线程");
            char c = scanner.next().toUpperCase().charAt(0);
            if(c == 'Q'){
                a.setLoop( false);
                System.out.println("线程B结束");
                break;
            }
        }
    }
}